import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';
import { NzMessageService } from 'ng-zorro-antd';
import { Observable, Observer } from 'rxjs';

declare var form: any;

@Component({
  templateUrl: './user.info.component.html'
})
export class UserInfoComponent implements OnInit {

  // 当前数据
  fs: any;
  ffs: any = null;

  // 字典
  airDic: any = null;
  regulatoryDic: any = null;
  depDic: any = null;
  sexDic: any = null;
  userTypeDic: any = null;

  operationDic: any = null;
  positionDic: any = null;
  workingGroupDic: any = null;
  roleDic: any = null;
  adminDic: any = null;

  // 证书类型
  zsTypeDic: any = null;

  constructor(
    public msg: NzMessageService,
    public route: ActivatedRoute,
    private modalService: NzModalService) {
    // 注册表单
    this.fs = new form.FormSimpleService('userinfo', {
      orderObject: { userid: 0 },
      keyField: 'userid',
      randomFields: ['userid']
    });
    // 文件服务
    this.ffs = new form.FileService();

    // 初始化字典

    this.airDic = new form.DictionaryService('p_department');
    this.airDic.loadAll();

    this.depDic = new form.DictionaryService('dic_department');
    this.depDic.loadAll();



    this.sexDic = new form.DictionaryService('p_sex');
    this.userTypeDic = new form.DictionaryService('p_user_type');

    this.operationDic = new form.DictionaryService('p_operating_post');
    this.positionDic = new form.DictionaryService('p_position');
    this.workingGroupDic = new form.DictionaryService('p_working_group');

    this.roleDic = new form.DictionaryService('p_role');
    this.roleDic.loadAll();
    this.adminDic = new form.DictionaryService('p_administrative');

    this.zsTypeDic = new form.DictionaryService('p_user_zslx');
    this.zsTypeDic.loadAll();


  }

  ngOnInit() {
    this.initData();

    // 浏览器关闭监
    window.onbeforeunload = () => {
      // tslint:disable-next-line: deprecation
      const e: any = window.event;
      e.returnValue = ('请确认已保存，是否离开？');
      return e;
    };
  }

  initData() {
    let id = '-1';
    this.route.queryParams.subscribe((res) => {
      if (res.id) {
        id = res.id;
      }
    });
    this.fs.editRowByKey(id);
  }

  onPhonepress(event) {
    // 判断是否为数字
    const inputStr = String.fromCharCode(event.keyCode);
    const re = /^[0-9]+.?[0-9]*$/; // 判断字符串是否为数字
    if (!re.test(inputStr)) {
      return false;
    }
  }

  save() {
    const er = this.fs.er;
    debugger
    if (!er.employeetype || er.employeetype === ''
      || !er.userName || er.userName === ''
      || !er.userid || er.userid === ''
      || !er.enterTime || er.enterTime === ''
      || !er.tel || er.tel === ''
      || !er.departmentId || er.departmentId === '') {
      this.modalService.success({
        nzTitle: '系统提示',
        nzContent: '工号，用户名，用户类型，入职时间，联系电话，部门必填!'
      });
      return;
    }

    this.fs.save(er, (data) => {
      this.modalService.success({
        nzTitle: '系统提示',
        nzContent: '保存成功!'
      });
      this.fs.er = data;
    }, () => {
      this.modalService.error({
        nzTitle: '系统提示',
        nzContent: '保存失败!'
      });
    });
  }

  close() {
    window.close();
  }

  // 头像上传前处理
  imageCheck = (file: File) => {
    return new Observable((observer: Observer<boolean>) => {
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isLt2M) {
        this.msg.error('文件不可大于1MB!');
        observer.complete();
        return;
      }
      // check height
      this.checkImageDimension(file).then(dimensionRes => {
        if (!dimensionRes) {
          this.msg.error('图片只支持300x300大小!');
          observer.complete();
          return;
        }
        observer.next(isLt2M && dimensionRes);
        observer.complete();
      });
    });
  }

  private checkImageDimension(file: File): Promise<boolean> {
    return new Promise(resolve => {
      const img = new Image(); // create image
      img.src = window.URL.createObjectURL(file);
      img.onload = () => {
        const width = img.naturalWidth;
        const height = img.naturalHeight;
        resolve(height <= 300 || width <= 300);
      };
    });
  }

  genggai($event: any) {
    debugger
  }
}
